/**
 * Reloads the content of the page found to the passed URL
 *
 * @param url
 *            A URL
 * @return void
 */
function reloadContent(url) {
	window.location = url;
}

/**
 * Binds events to all the editable inputs of the passed form so that the button
 * denoted by the passed ID is enabled only if modifications have been made in
 * the form values.
 *
 * @param buttonId
 *            A button ID
 * @return void
 */
function handleSubmitButtonStateForForm(buttonId) {

	var button = $("#" + buttonId);
	var form = button.parents("form");

	// adding events to each editable field in the form
	var fields = form.find("input:text,input:radio,"
			+ "input:checkbox,input:file, select");

	// storing the initial values
	var initialValues = new Array();
	fields.each(function(index, el) {
		var field = $(el);
		initialValues[index] = field.val();
	});

	// the function handling the button state according to the modified value
	var checkModifFn = function() {

		var modified = false;

		fields.each(function(index, el) {

			var field = $(el);
			var newVal = field.val();

			if (initialValues[index] != newVal) {

				modified = true;
			}
		});

		setFormElementEnabled(button, modified);
	};

	fields.change(checkModifFn);
	fields.keyup(checkModifFn);
	fields.mouseleave(checkModifFn);
}

/**
 * Modifies the activation state of the form element denoted by the passed ID.
 *
 * @param formEl
 *            A form element.
 * @param enabled
 *            Whether the form element should be activated or not.
 * @return void
 */
function setFormElementEnabled(formEl, enabled) {

	var disabledClass = "disabled";

	if (enabled) {

		formEl.removeClass(disabledClass);
		formEl.removeAttr(disabledClass);

	} else {

		formEl.addClass(disabledClass);
		formEl.attr(disabledClass, disabledClass);
	}

	mergeClasses(formEl);
}

/**
 * Creates a new class for the element, that is the concatenation of all its
 * classes, if it contains more than one.
 *
 * @param el
 *            An element
 * @return void
 */
function mergeClasses(el) {

	if (isIE6()) {

		var mergedClassPrefix = "M_";
		var classes = el.attr("class");
		var splitClasses = classes.split(' ');
		var className = null;
		var mergedClassName = mergedClassPrefix;
		var mergedClasses = 0;

		for ( var i = 0; i < splitClasses.length; i++) {

			className = splitClasses[i];

			if (className != null && className != '' && el.hasClass(className)) {

				if (className.indexOf(mergedClassPrefix) >= 0) {

					el.removeClass(className);

				} else {

					mergedClassName += (mergedClasses > 0 ? "-" : "")
					+ className;
					mergedClasses++;
				}
			}
		}

		if (mergedClasses > 1) {

			el.addClass(mergedClassName);
		}
	}
}

/**
 * Modifies the activation state of the form elements denoted by the passed IDs.
 *
 * @param formElementIds
 *            The array of the form element IDs.
 * @param enabled
 *            Whether the form elements should be activated or not.
 * @return
 */
function setFormElementsEnabled(formElementIds, enabled) {

	for ( var i = 0; i < formElementIds.length; i++) {

		setFormElementEnabled($("#" + formElementIds[i]), enabled);
	}
}

/**
 * Modifies the state (visible/hidden) of the inputs stored in the passed array
 * (inputIdsToModify), according to the current value of the input denoted by
 * the "refInputId" ID. If the current values matches one of the passed values,
 * the inputs are visible, otherwise, they are hidden.
 *
 * @param refInputId
 *            The ID of an input.
 * @param acceptedValues
 *            The accepted values for the input, that triggers the other inputs
 *            activation.
 * @param inputIdsToModify
 *            The IDs of the inputs to modify.
 * @return void
 */
function setInputsVisible(refInputId, acceptedValues, inputIdsToModify) {

	var refInput = $("#" + refInputId);
	var curVal = refInput.val();

	// checking if the accepted values array contains the current value
	var contains = false;

	for ( var i = 0; i < acceptedValues.length; i++) {

		if (acceptedValues[i] == curVal) {

			contains = true;
			break;
		}
	}

	// modifying each input state
	setFormElementsVisible(inputIdsToModify, contains);
}

/**
 * Modifies the visible state of the form element denoted by the passed ID.
 *
 * @param formEl
 *            A form element.
 * @param visible
 *            Whether the form element should be visible or not.
 * @return void
 */
function setFormElementVisible(formEl, visible) {

	var hiddenClass = "hidden";

	if (visible) {

		formEl.removeClass(hiddenClass);

	} else {

		formEl.addClass(hiddenClass);
	}
}

/**
 * Modifies the visible state of the form elements denoted by the passed IDs.
 *
 * @param formElementIds
 *            The array of the form element IDs.
 * @param visible
 *            Whether the form elements should be visible or not.
 * @return
 */
function setFormElementsVisible(formElementIds, visible) {

	for ( var i = 0; i < formElementIds.length; i++) {

		setFormElementVisible($("#" + formElementIds[i]), visible);
	}
}

/**
 * Checks if the session is down and then, reloads the page.
 *
 * @param container
 *            The container into which special tags should be searched to known
 *            if the session is down.
 * @return void
 */
function checkIfSessionDown(container) {

	if (container.find("#loginForm").length > 0) {

		window.location = window.location;
	}
}

/**
 * Updates the tickets count
 *
 * @param url
 *            The URL to retrieve the tickets count
 * @return void
 */
function updateTicketsCount(url) {

	$("#ticketsCount").load(url);
}

/**
 * Sets the current language to the passed one.
 *
 * @param lang
 *            A language code
 * @param url
 *            The url to use to modify the language. If null, the page URL is
 *            used
 * @return void
 */
function setLang(lang, url) {

	if (url == null || url == "") {

		url = window.location + "";
	}

	var paramsStart = '?';
	var paramStart = '&';

	// removing the locale parameter, if necessary
	var localeParam = "locale";
	var localeParamIndex = url.indexOf(localeParam, 0);

	if (localeParamIndex != -1) {

		// computing the url start part
		var urlStart = url.substring(0, localeParamIndex - 1);

		// computing the url end part
		var urlEnd = "";
		var localeParamEndIndex = url.indexOf(paramStart, localeParamIndex);

		if (localeParamEndIndex != -1) {

			urlEnd = url.substring(localeParamEndIndex, url.length);
		}

		if (urlEnd.length > 0 && urlStart.indexOf(paramsStart) == -1) {

			urlStart += paramsStart;

			if (urlEnd.charAt(0) == paramStart) {

				urlEnd = urlEnd.substring(1, urlEnd.length);
			}
		}

		url = urlStart + urlEnd;
	}

	// computing the new parameter value for the locale
	var prefix = paramsStart;

	if (url.indexOf(prefix, 0) != -1) {

		prefix = paramStart;
	}

	url = url + prefix + "locale=" + lang;

	window.location = url;
}

/**
 * Inits the selection of sortable columns of the passed table ID.
 *
 * @param tableId
 *            A table ID.
 * @return void
 */
function initSortableCols(tableId) {

	$("#" + tableId + " th.sortable a").click(function(event) {
		event.preventDefault();
	});

	$("#" + tableId + " th.sortable").click(function(event) {
		event.stopPropagation();
		window.location = $(this).find("a").attr("href");
	});
}

/**
 * Inits the selection of row cells of the passed table ID. The href attribute
 * of the table cell link is used for the actions.
 *
 * @param tableId
 *            A table ID.
 * @return void
 */
function initTableRowSelWithHref(tableId) {

	var links = $(".actionsOnRows td>a");
	links.parents("td").addClass("nospace");
	links.hover(function() {
		$(this).parent("td").parent("tr").find("a").addClass("rowHover");
	}, function() {
		$(this).parent("td").parent("tr").find("a").removeClass("rowHover");
	});
}

/**
 * Inits the selection of row cells of the passed table ID. The onclick
 * attribute of the table cell link is used for the actions.
 *
 * @param tableId
 *            A table ID.
 * @return void
 */
function initTableRowSelWithOnclick(tableId) {

	$("#" + tableId + " td a").click(function(event) {
		event.preventDefault();
	});

	$("#" + tableId + " td:not(.check)").click(function(event) {
		event.stopPropagation();
		var td = $(this);
		td.attr("onclick", td.find("a").attr("onclick"));
	});
}

/**
 * @return Whether the browser is IE6
 */
function isIE6() {

	return $.browser.msie && jQuery.browser.version == '6.0';
}

/**
 * @return Whether the browser is IE7
 */
function isIE7() {

	return $.browser.msie && jQuery.browser.version == '7.0';
}

/**
 * Hides/Shows the element matching the passed ID.
 *
 * @param id The element ID
 * @param show Whether it should be shown or not.
 */
function hideShow(id, show){

	if(show == true){
		$("#" + id).show();
	}
	else{
		$("#" + id).hide();
	}
}

/**
 * Shows/Hides the product types according to the passed value.
 *
 * @param val The value
 */
function showProductTypes(val){

	if( val == "CLOTHING_GIRL"){
		hideShow("productFeatures", true);
		hideShow("GIRL", true);
		hideShow("BOY", false);
		hideShow("MAN", false);
		hideShow("WOMAN", false);
	}
	else if( val == "CLOTHING_BOY"){
		hideShow("productFeatures", true);
		hideShow("BOY", true);
		hideShow("GIRL", false);
		hideShow("MAN", false);
		hideShow("WOMAN", false);
	}
	else if( val == "CLOTHING_MAN"){
		hideShow("productFeatures", true);
		hideShow("MAN", true);
		hideShow("BOY", false);
		hideShow("GIRL", false);
		hideShow("WOMAN", false);
	}
	else if( val == "CLOTHING_WOMAN"){
		hideShow("productFeatures", true);
		hideShow("WOMAN", true);
		hideShow("BOY", false);
		hideShow("MAN", false);
		hideShow("GIRL", false);
	}
	else{
		hideShow("productFeatures", false);
	}
}


/**
 * Shows/Hides the product types according to the passed value.
 *
 * @param val The value
 */
function showDetail(val){

	if( val == "CLOTHING_GIRL"){
		hideShow("CLOTHING_GIRL", true);
		hideShow("CLOTHING_BOY", false);
		hideShow("CLOTHING_MAN", false);
		hideShow("CLOTHING_WOMAN", false);
	}
	else if( val == "CLOTHING_BOY"){
		hideShow("CLOTHING_GIRL", false);
		hideShow("CLOTHING_BOY", true);
		hideShow("CLOTHING_MAN", false);
		hideShow("CLOTHING_WOMAN", false);
	}
	else if( val == "CLOTHING_MAN"){
		hideShow("CLOTHING_GIRL", false);
		hideShow("CLOTHING_BOY", false);
		hideShow("CLOTHING_MAN", true);
		hideShow("CLOTHING_WOMAN", false);
	}
	else if( val == "CLOTHING_WOMAN"){
		hideShow("CLOTHING_GIRL", false);
		hideShow("CLOTHING_BOY", false);
		hideShow("CLOTHING_MAN", false);
		hideShow("CLOTHING_WOMAN", true);
	}
	else{
		hideShow("CLOTHING_GIRL", false);
		hideShow("CLOTHING_BOY", false);
		hideShow("CLOTHING_MAN", false);
		hideShow("CLOTHING_WOMAN", false);
	}
}

/**
 * The main page container ID
 */
var WHOLE_CONTAINER_ID = "bodyContent";


/**
 * Inits a popup box with the content of the element denoted by the passed ID.
 *
 * @param widenedMapID
 *            The widen map ID.
 * @param title
 *            The title for the box
 * @return void
 */
function initWidenedMapBox(widenedMapBox, widenedMapID, title, closeBtFn) {

	var container = $("body");
	var box = $("#" + widenedMapBox);

	// setting the box type
	box.removeAttr("class").addClass("confirmBodyBox widenedMapBox");

	// setting the new title
	var boxTitle = box.find("div.boxCnt>p.boxTitle").empty().append(
			'<span>' + title + '</span>');

	// setting the way to hide the box
	$('<span class="closeBt">&nbsp;</span>').prependTo(boxTitle).click(
			function() {
				box.hide();
			});

	// turning the box into a draggable one
	var boxCnt = box.find("div.boxCnt");
	boxCnt.draggable({
		disabled : false,
		appendTo : 'body',
		containment : [ 0, 0, $(window).height(), $(window).width() ],
		scroll : false,
		zIndex : 100000,
		handle : 'p.boxTitle',
		refreshPositions : false
	});

	box.hide();
}

/**
 * * Widens the map.
 *
 * @param widenedMapID
 *            The widen map ID.
 * @param attachOnClickEvent
 *            Whether attach the onclick event or not.
 * @return void
 */
function widenMap(widenedMapBox, widenedMapID, attachOnClickEvent, draggable) {

	var container = $("body");
	var box = $("#" + widenedMapBox);

	// adding the box to the container
	container.append(box);

	// showing the box
	box.show();
}
